﻿using System;
using System.Text;
using System.IO;
using System.Net;
using System.Web;

namespace TwitterWrapper
{
    public class TwitterCom
    {
        internal static Stream HTTPGet(string url)
        {
            return HTTPGet(url,null,null);
        }

        internal static Stream HTTPGet(string url, string name, string password)
        {
            WebClient client = new WebClient();
            if (!string.IsNullOrEmpty(name)&&!string.IsNullOrEmpty(password))
            {
                client.Credentials = new NetworkCredential(name, password);
            }

            try
            {
                return client.OpenRead(url);
            }
            catch
            {
                return null;
            }
        }

        internal static Stream HTTPPost(string url,string name, string password)
        {
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
            {
                throw new Exception("Username or Password can't be null or empty.");
            }
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.Credentials = new NetworkCredential(name, password);

            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;
            request.ContentLength = 0;

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return response.GetResponseStream();
            }
            catch
            {
                return null;
            }
        }
    }
}

